﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ComponentModel;
using Didsoft.SalesLive.Entidades;
using System.Collections.ObjectModel;
using Didsoft.Silverlight.ViewModel.Commanding.Contracts;
using Didsoft.Silverlight.ViewModel;
using System.Linq;
using System.Collections.Generic;
using System.ServiceModel.DomainServices.Client;


namespace Didsoft.SalesLive.UI.Views.ViewModel
{
    public class VmRoles : ViewModelBase
    {
         #region Entity Framework Servidor Negocios

        private Negocios.RolNegocios negocios = new Negocios.RolNegocios();
        
        #endregion

        #region propiedades bindables

        private Rol rol;
        public Rol Rol
        {
            get { return rol; }
            set
            {
                rol = value;
                Notificar("Rol");
                CmdEliminar.RaiseCanExecuteChanged();
            }
        }
        
        public IEnumerable<Rol> Roles
        {
            get { return negocios.Rols; }
        }
        

        #endregion

        #region comandos

        #region CmdNuevo

        public ICommandEvent CmdNuevo { get; set; }
        public void CmdNuevo_Ejecutar(string param)
        {
            Rol = new Rol();
            CmdGuardar.RaiseCanExecuteChanged();

        }
        public bool CmdNuevo_PuedeEjecutar(string param)
        {
            return true;
        }

        #endregion

        #region CmdGuardar

        public ICommandEvent CmdGuardar { get; set; }
        public void CmdGuardar_Ejecutar(string param)
        {
            

            if(Rol.Id==0) negocios.Rols.Add(Rol);
            Guardar();

            Rol = null;
            CmdGuardar.RaiseCanExecuteChanged();

        }
        public bool CmdGuardar_PuedeEjecutar(string param)
        {
            return Rol != null;
        }

        #endregion        

        #region CmdEliminar

        public ICommandEvent CmdEliminar { get; set; }
        public void CmdEliminar_Ejecutar(string param)
        {
            negocios.Rols.Remove(Rol);
            Guardar();
        }
        public bool CmdEliminar_PuedeEjecutar(string param)
        {
            return Rol!=null && negocios.Rols.Contains(Rol);
        }

        #endregion

        #endregion

       
        
        #region constructor

        public VmRoles()
        {
            InicializarComandos();
            CargarRoles();
        }       

        #endregion

        #region metodos

        private void CargarRoles()
        {
            negocios.Load<Rol>(negocios.GetRolesQuery(),
                                cargar =>
                                {
                                    if (cargar.HasError)
                                        cargar.MarkErrorAsHandled();
                                    
                                }, null);
        }

        private void InicializarComandos()
        {
            CmdNuevo = new DelegateCommand<string>(CmdNuevo_Ejecutar, CmdNuevo_PuedeEjecutar);
            CmdGuardar = new DelegateCommand<string>(CmdGuardar_Ejecutar, CmdGuardar_PuedeEjecutar);
            CmdEliminar = new DelegateCommand<string>(CmdEliminar_Ejecutar, CmdEliminar_PuedeEjecutar);
        }

       

        private void Guardar()
        {
            negocios.SubmitChanges(OnSubmitCompleted, null);        
        }

        private void OnSubmitCompleted(SubmitOperation so)
        {
            if (so.HasError)
            {
                MessageBox.Show(string.Format("Submit Failed: {0}", so.Error.Message));
                so.MarkErrorAsHandled();
            }
            //CheckChanges();
        }

        #endregion

       

    }


}
